/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "GPCControllerDesigner.h"
#include "ui_GPCControllerDesigner.h"

#include <QMessageBox>

#include <sstream>

using namespace std;
using namespace RafMat::SimInterface;

GPCControllerDesigner::GPCControllerDesigner(QWidget *parent) :
    ControllerDesigner(parent),
    ui(new Ui::GPCControllerDesigner)
{
    ui->setupUi(this);
}

GPCControllerDesigner::~GPCControllerDesigner()
{
    delete ui;
}

void GPCControllerDesigner::attachController(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_controllerConf = ctrl;

    try
    {
        ui->gpcH->setValue(QString::fromStdString(m_controllerConf->getParameter("h")).toDouble());
        ui->gpcL->setValue(QString::fromStdString(m_controllerConf->getParameter("l")).toDouble());
        ui->gpcRho->setValue(QString::fromStdString(m_controllerConf->getParameter("rho")).toDouble());
        ui->gpcAlpha->setValue(QString::fromStdString(m_controllerConf->getParameter("alpha")).toDouble());
        ui->gpcDA->setValue(QString::fromStdString(m_controllerConf->getParameter("da")).toDouble());
        ui->gpcDB->setValue(QString::fromStdString(m_controllerConf->getParameter("db")).toDouble());
        ui->gpcDelay->setValue(QString::fromStdString(m_controllerConf->getParameter("dly")).toDouble());
        ui->gpcGain->setValue(QString::fromStdString(m_controllerConf->getParameter("gain")).toDouble());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void GPCControllerDesigner::applySettings()
{
    try
    {
        ostringstream ostr;

        ostr << "[ ";
        ostr << ui->gpcH->value() << " ";
        ostr << ui->gpcL->value() << " ";
        ostr << ui->gpcRho->value() << " ";
        ostr << ui->gpcAlpha->value() << " ";
        ostr << ui->gpcDA->value() << " ";
        ostr << ui->gpcDB->value() << " ";
        ostr << ui->gpcDelay->value() << " ";
        ostr << ui->gpcGain->value() << " ]";

        m_controllerConf->setParameter("*", ostr.str());
    }
    catch(iconf_error& e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void GPCControllerDesigner::on_gpcH_valueChanged(int arg1)
{
    ui->gpcL->setMaximum(arg1);
}
